/**
 * @ Author 何涛
 * @ Date 2016/02/19 01:08:47
 */

gpNamespace.SoundManager = cc.Class.extend({
    playEffect: function (res, loop) {
        var effectID;
        if (ht_config.isEffect && !ht_config.isBackground) {
            effectID = cc.audioEngine.playEffect(res, loop);
        }

        return effectID;
    },

    playMusic: function (res, loop) {
        if (ht_config.isMusic && !ht_config.isBackground) {
            if (!cc.audioEngine.isMusicPlaying())
                cc.audioEngine.playMusic(res, loop);
            else
                cc.audioEngine.resumeMusic();
        }
    },

    pauseMusic: function () {
        if (ht_config.isMusic && !ht_config.isBackground) {
            if (cc.audioEngine.isMusicPlaying())
                cc.audioEngine.pauseMusic();
        }
    },

    //同步前一次声音记录
    setEffectVolumn: function (percent) {
        if (ht_config.isEffect && !ht_config.isBackground) {
            cc.audioEngine.setEffectsVolume(percent / 100);
            this.setCurrentEffectVolumn(percent);
            this.setLastEffectVolumn();
        }
    },

    setCurrentEffectVolumn: function (percent) {
        gpNamespace.GameConfig.EFFECTSOUND = percent;
    },

    getCurrentEffectVolumn: function () {
        return gpNamespace.GameConfig.EFFECTSOUND;
    },

    setLastEffectVolumn: function () {
        gpNamespace.GameConfig.LASTEFFECTSOUND = gpNamespace.GameConfig.EFFECTSOUND;
    },

    getLastEffectVolumn: function () {
        return gpNamespace.GameConfig.LASTEFFECTSOUND;
    },

    minEffectVolumn: function () {
        this.setEffectVolumn(0);
    },

    maxEffectVolumn: function () {
        this.setEffectVolumn(100);
    },

    //不同步前一次声音记录
    offEffectVolumn: function () {
        if (ht_config.isEffect && !ht_config.isBackground) {
            cc.audioEngine.setEffectsVolume(0);
            this.setCurrentEffectVolumn(0);
        }
    },

    onEffectVolumn: function () {
        if (ht_config.isEffect && !ht_config.isBackground) {
            var sound = this.getLastEffectVolumn();
            cc.audioEngine.setEffectsVolume(sound / 100);
            this.setCurrentEffectVolumn(sound);
        }
    },

    //同步前一次声音记录
    setMusicVolume: function (percent) {
    	if (ht_config.isMusic && !ht_config.isBackground) {
    		var result = percent / 100;
    		cc.audioEngine.setMusicVolume(result);
            this.setCurrentMusicVolumn(percent);
            this.setLastMusicVolumn();
        }
    },

    setCurrentMusicVolumn: function (percent) {
        gpNamespace.GameConfig.MUSICSOUND = percent;
    },

    getCurrentMusicVolumn: function () {
        return gpNamespace.GameConfig.MUSICSOUND;
    },

    setLastMusicVolumn: function () {
        gpNamespace.GameConfig.LASTMUSICSOUND = gpNamespace.GameConfig.MUSICSOUND;
    },

    getLastMusicVolumn: function () {
        return gpNamespace.GameConfig.LASTMUSICSOUND;
    },

    minMusicVolumn: function () {
        this.setMusicVolume(0);
    },

    maxMusicVolumn: function () {
        this.setMusicVolume(100);
    },

    //不同步前一次声音记录
    offMusicVolumn: function () {
        if (ht_config.isMusic && !ht_config.isBackground) {
            cc.audioEngine.setMusicVolume(0);
            this.setCurrentMusicVolumn(0);
        }
    },

    onMusicVolumn: function () {
        if (ht_config.isEffect && !ht_config.isBackground) {
            var sound = this.getLastMusicVolumn();
            cc.audioEngine.setEffectsVolume(sound / 100);
            this.setCurrentMusicVolumn(sound);
        }
    }
});

gpNamespace.SoundManager = new gpNamespace.SoundManager();